﻿using BCCommon;
using BCCommon.FreightEnums;
using BCDto.Common.Area;
using BCService.Common.Area;
using BCService.Utils;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.Area
{
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class ProvincesController : ManagementBaseController
    {
        private readonly IProvinceService provinceService;

        public ProvincesController(IProvinceService provinceService)
        {
            this.provinceService = provinceService;
        }
      
        /// <summary>
        /// 获取省列表
        /// </summary>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <param name="provinceCode">省Id</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<ProvinceDto>), 200)]
        public IActionResult Get([FromQuery(Name = "province_code")]string provinceCode,[FromQuery(Name = "page_index")]int index = 1, [FromQuery(Name = "page_size")]int size = 10)
        {
            return Ok(provinceService.GetProvinces(provinceCode,index, size));
        }
       
        /// <summary>
        /// 获取省信息
        /// </summary>
        /// <param name="provinceCode">省Id</param>
        /// <returns></returns>
        [HttpGet("{province_code}")]
        [ProducesResponseType(typeof(ProvinceDto), 200)]
        public IActionResult Get([FromRoute(Name = "province_code")]string provinceCode)
        {
            return Ok(provinceService.GetProvinceEntity(provinceCode));
        }
       
        /// <summary>
        /// 增加省信息
        /// </summary>
        /// <param name="dto">省信息模型</param>
        /// <returns></returns>
        [HttpPost]
        [Log("添加省份信息")]
        [ProducesResponseType(typeof(ProvinceDto), 201)]
        public IActionResult Post([FromBody]ProvinceRequestDto dto)
        {
            if (provinceService.Add(dto,this.Admin))
            {
                return Created("GetProvinceById", provinceService.GetProvinceEntity(dto.ProvinceCode));
            }
            else {
                return BadRequest("添加省份信息失败");
            }
        }

        /// <summary>
        /// 修改省信息
        /// </summary>
        /// <param name="provinceId">省Id</param>
        /// <param name="dto">省信息模型</param>
        /// <returns></returns>
        [HttpPut("{province_id}")]
        [Log("修改指定省份信息")]
        [ProducesResponseType(typeof(ProvinceDto), 200)]
        public IActionResult Put([FromRoute(Name = "province_id")]long provinceId, [FromBody]ProvinceRequestDto dto)
        {
            ProvincePutDto putDto = dto.As<ProvincePutDto>();
            putDto.ProvinceId = provinceId;
            provinceService.Update(putDto,this.Admin);
            return StatusCode((int)HttpStatusCode.OK, this.provinceService.GetProvinceEntity(provinceId));
        }
     
        /// <summary>
        /// 删除省信息
        /// </summary>
        /// <param name="provinceCode">省Id</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpDelete("{province_code}")]
        [Log("删除指定省份信息")]
        public IActionResult Delete([FromRoute(Name = "province_code")]string provinceCode)
        {
            provinceService.Delete(provinceCode);
            return NoContent();
        }

        /// <summary>
        /// 修改省份需要议价
        /// </summary>
        /// <param name="provinceCode">省Id</param>
        /// <param name="businessType">业务类型</param>
        /// <param name="isNegotiatedPrice">需要议价</param>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpPut("[action]/{province_code}")]
        [Log("修改省份议价状态")]
        public IActionResult NegotiatedPrice([FromRoute(Name = "province_code")]string provinceCode,
            [FromQuery(Name = "business_type")]FreightBusinessType businessType, 
            [FromQuery(Name = "is_negotiated_price")]bool isNegotiatedPrice)
        {
            provinceService.UpdateProvinceNegotiatedPrice(provinceCode, businessType, isNegotiatedPrice);
            return this.NoContent();
        }
    }
}
